package cn.swenty.backstage.base.web.system.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import cn.swenty.backstage.base.web.system.entity.SysUserFormMap;
import cn.swenty.backstage.base.web.system.entity.SysUserRoleFormMap;
import cn.swenty.backstage.base.web.system.mapper.UserInfoMapper;
import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.service.BaseService;
import cn.swenty.backstage.framework.util.IdGenerator;
import cn.swenty.backstage.framework.util.PasswordHelper;

@Service
public class UserInfoService extends BaseService {
		
		@Inject
		private  UserInfoMapper userInfoMapper;
		
		
		/**
		 * 新增用户
		 * @param userFormMap
		 * @param txtGroupsSelect
		 * @throws RuntimeException
		 */
		public void addEntity(SysUserFormMap userFormMap)throws RuntimeException{
		try {
			userFormMap.set("password","888888");
			String password = PasswordHelper.encryptPassword((String)userFormMap.get("password"));
			userFormMap.put("password",password);
			userFormMap.put("user_id", IdGenerator.getNextId());
			this.save(userFormMap);// 新增后返回新增信息
			
			
			SysUserRoleFormMap sysUserRoleFormMap= new SysUserRoleFormMap();   //用户角色关联表
			sysUserRoleFormMap.put("id", IdGenerator.getNextId());
			sysUserRoleFormMap.put("user_id",userFormMap.get("user_id") );
			sysUserRoleFormMap.put("role_id", "everyone");   //everyone 角色
			this.save(sysUserRoleFormMap);  //默认角色
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
		
		
		
		/**
		 * 编辑用户信息
		 * @param userFormMap
		 * @param txtGroupsSelect  
		 */
		public void editEntit(SysUserFormMap userFormMap)throws RuntimeException{
			try {
				userInfoMapper.update(userFormMap);//修改用户信息
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		
		/**
		 * 编辑用户信息 (逻辑删除)
		 * @param userFormMap
		 * @param txtGroupsSelect  
		 */
		public void editEntits(SysUserFormMap userFormMap)throws RuntimeException{
			try {
				userInfoMapper.editEntitys(userFormMap);//修改用户信息
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		
		/**
		 * 根据角色id查询用户
		 * @param userRole
		 * @return
		 */
		public List<SysUserFormMap> findUserByRoleId(SysUserRoleFormMap userRole){
			try {
				return userInfoMapper.findUserByRoleId(userRole);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			
		}
		
		
		/**
		 * 增加用户角色关系
		 * @throws Exception 
		 */
		public void saveUserRole(List<String> list,String userId) throws Exception{
			//先删除
				SysUserRoleFormMap sysUserRoleFormMap= new SysUserRoleFormMap();
				sysUserRoleFormMap.put("user_id", userId);
				userInfoMapper.deleteUserRole(sysUserRoleFormMap);//删除原有关系
			//后增加
			for (String string : list) {
				SysUserRoleFormMap userRoleFormMap= new SysUserRoleFormMap();
				userRoleFormMap.put("role_id", string);
				userRoleFormMap.put("user_id", userId);
				userRoleFormMap.put("id", IdGenerator.getNextId());
				userRoleFormMap.save();
			}
		}
		
		
		
		public SysUserFormMap findbyFrist(SysUserFormMap userFormMap){
			return userInfoMapper.findbyFrist(userFormMap);
		}
		
		
		public List<SysUserFormMap> findUserPage(SysUserFormMap userFormMap){
			return userInfoMapper.findUserPage(userFormMap);
		}
		
		
		/**
		 * 
		 * 根据一个根节点查询这个根节点下是领导的数据
		 * @param leadId
		 * @return
		 */
		public List<SysUserFormMap> findLeaderUser(String leadId){
			 List<SysUserFormMap> formMaps = findUser(leadId);
			 for(int i=0;i<formMaps.size();i++){
				 boolean flag = false;
				 for(SysUserFormMap formMap:formMaps){
					 if(formMap.getStr("leader_id")!=null && formMaps.get(i).getStr("user_id").equals(formMap.getStr("leader_id"))){
						 flag = true;
						 break;
					 }
				 }
				 if(!flag){
					 formMaps.remove(i);
					 i--;
				 }
			 }return formMaps;
		}
		
		public List<SysUserFormMap> findDownUser(String  leadId){
			return findUser(leadId);
		}
		
		public List<SysUserFormMap> findUser(String  leadId){
			SysUserFormMap userFormMap = new SysUserFormMap();
			userFormMap.put("user_id", leadId);
			//获得lead 对象
			userFormMap = userInfoMapper.findbyFrist(userFormMap);
			String userId= (String) userFormMap.get("user_id");
			List<SysUserFormMap>	userFormMaps = new ArrayList<SysUserFormMap>();
			userFormMaps.add(userFormMap);
			//向下查询所有集合
			List<SysUserFormMap> users = findDownUser(userFormMaps, userId);
			return users;
		}
		
		
		public List<SysUserFormMap> findDownUser(List<SysUserFormMap> userFormMaps, String userId){
			if(userFormMaps == null) {}
			SysUserFormMap userFormMap= new SysUserFormMap();
			userFormMap.put("leader_id", userId);
			userFormMap.put("state", 0);
			//下属
			List<SysUserFormMap>  nexUser=userInfoMapper.findUserPage(userFormMap);
			
			userFormMaps.addAll(nexUser);
			for (SysUserFormMap sysUserFormMap : nexUser) {
				findDownUser(userFormMaps, sysUserFormMap.getStr("user_id"));
			}
			
			return userFormMaps;
			
		}
		
		
		
		
		
		
		
		public List<SysUserFormMap> findUserByLeader(String  userId){
			SysUserFormMap userFormMap = new SysUserFormMap();
			userFormMap.put("user_id", userId);
			//获得lead 对象
			userFormMap = userInfoMapper.findbyFrist(userFormMap);
			String leader_id= (String) userFormMap.get("leader_id");
			List<SysUserFormMap>	userFormMaps = new ArrayList<SysUserFormMap>();
			userFormMaps.add(userFormMap);
			//向上查询所有集合
			List<SysUserFormMap> users = findUpUsers(userFormMaps, leader_id);
			return users;
		}
		
		
		public List<SysUserFormMap> findUpUsers(List<SysUserFormMap> userFormMaps, String leader_id){
			if(userFormMaps == null) {}
			SysUserFormMap userFormMap= new SysUserFormMap();
			userFormMap.put("user_id", leader_id);
			userFormMap.put("state", 0);
			//下属
			List<SysUserFormMap>  nexUser=userInfoMapper.findUserPage(userFormMap);
			
			userFormMaps.addAll(nexUser);
			for (SysUserFormMap sysUserFormMap : nexUser) {
				findUpUsers(userFormMaps, sysUserFormMap.getStr("leader_id"));
			}
			
			return userFormMaps;
			
		}
		
		
		
		
		
		
		public List<SysUserFormMap> findTeamUser(String  leadId){
			SysUserFormMap userFormMap = new SysUserFormMap();
			userFormMap.put("", leadId);
			//获得lead 对象
			userFormMap = userInfoMapper.findbyFrist(userFormMap);
			String userId= (String) userFormMap.get("user_id");
			List<SysUserFormMap>	userFormMaps = new ArrayList<SysUserFormMap>();
			userFormMaps.add(userFormMap);
			//向下查询所有集合
			List<SysUserFormMap> users = findDownUser(userFormMaps, userId);
			return users;
		}
		
		
		
		public List<SysUserFormMap> findUpUser(String  user_id){
			//向下查询所有集合
			List<SysUserFormMap> users =new ArrayList<SysUserFormMap>();
			SysUserFormMap userFormMap = new SysUserFormMap();
			userFormMap.put("user_id", user_id);
			//获得lead 对象
			userFormMap = userInfoMapper.findbyFrist(userFormMap);
			if(userFormMap != null){
				String userId= (String) userFormMap.get("leader_id");
				List<SysUserFormMap>	userFormMaps = new ArrayList<SysUserFormMap>();
				userFormMaps.add(userFormMap);
				users = findUsersByUp(userFormMaps, userId);
			}
		
			
			return users;
		}
		
		
		public List<SysUserFormMap> findUsersByUp(List<SysUserFormMap> userFormMaps, String leader_id){
			if(userFormMaps == null) {}
			if(leader_id ==null){
				return userFormMaps;
			}
			SysUserFormMap userFormMap= new SysUserFormMap();
			userFormMap.put("user_id", leader_id);
			userFormMap.put("state", 0);
			//上级
			List<SysUserFormMap>  nexUser=userInfoMapper.findUserPage(userFormMap);
			
			userFormMaps.addAll(nexUser);
			for (SysUserFormMap sysUserFormMap : nexUser) {
				findUsersByUp(userFormMaps, sysUserFormMap.getStr("leader_id"));
			}
			
			return userFormMaps;
			
		}
		
		public List<SysUserFormMap>  findByUserPage(SysUserFormMap sysUserFormMap){
			return userInfoMapper.findByUserPage(sysUserFormMap);
		}
		
		public List<SysUserFormMap> findUserByDeptId(SysUserFormMap userFormMap){
			return userInfoMapper.findUserByDeptId(convertFormMap(userFormMap));
		}
		
		public SysUserFormMap findByJobId(String job_id){
			if(StringUtil.isNull(job_id)){
				return null;
			}
			SysUserFormMap formMap = new SysUserFormMap();
			formMap.put("job_id", job_id);
			return this.getOne(userInfoMapper.findByField(formMap));
		}
		 
		public List<SysUserFormMap>  findByUser(String findByUser){
			return userInfoMapper.findByUser(findByUser);
		}
		
		public List<SysUserFormMap>  findUserByleaderId(String leaderId){
			SysUserFormMap user = new SysUserFormMap();
			user.put("leader_id", leaderId);
			return userInfoMapper.findUserByleaderId(user);
		}
		
		/**
		 * 查询该用户拥有的名单数
		 * @param userFormMap
		 * @return
		 */
		public List<SysUserFormMap>  findListNum(SysUserFormMap userFormMap){
			return userInfoMapper.findListNum(userFormMap);
		}
		
}
